package Algorithm.math;

import Top100.BigIntegerOperation;

/**
 * @author liujun
 * @version 1.0
 * @date 2020/5/15
 * @author—Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description 大整数相关操作
 */

public class ArrayBigIntegersOperation implements BigIntegerOperation {

    //大整数加法【带符号】
    public String sumWithSymbol(String args, String args2) {
        return "";
    }

    //大整数减法【带符号】
    public String subWithSymbol(String args, String args2) {
        return "";
    }

    //大整数乘法【带符号】
    public String multiWithSymbol(String args, String args2) {
        return "";
    }

    //大整数除法【带符号】
    public String divWithSymbol(String args, String args2) {
        return "";
    }

    //大整数合并【带符号】
    public String mergeWithSymbol(String args, String args2) {
        return "";
    }

    //大整数加法【无符号】
    public String sumWithoutSymbol(String args, String args2) {
        StringBuilder sb = new StringBuilder(args);
        StringBuilder sb2 = new StringBuilder(args2);
        //字符串逆置，方便计算和存储
        char[] ch = sb.reverse().toString().toCharArray();
        char[] ch2 = sb2.reverse().toString().toCharArray();
        // 找出最大位数的那个数
        if (ch2.length > ch.length) {
            char[] temp = ch2;
            ch2 = ch;
            ch = temp;
        }
        //加法可能会进位，需要重新构造一个StringBuilder
        StringBuilder sb3 = new StringBuilder();
        int decimal = 0;// 进位
        //运算,数值整理和进位
        int pisition = 0;
        int temp = 0;
        while (pisition < ch.length) {
            if (pisition < ch2.length) {
                temp = (ch[pisition] - '0') + (ch2[pisition] - '0') + decimal;
            } else {
                temp = ch[pisition] - '0' + decimal;
            }
            decimal = temp / 10;
            sb3.append((char) (temp % 10 + '0'));//tips:最后需要加上'0'
            pisition++;
        }
        if (decimal != 0) {
            sb3.append(decimal);
        }
        return sb3.reverse().toString();
    }

    //大整数减法【无符号】
    public String subWithoutSymbol(String args, String args2) {
        StringBuilder sb = new StringBuilder(args);
        StringBuilder sb2 = new StringBuilder(args2);
        //字符串逆置，方便计算和存储
        char[] ch = sb.reverse().toString().toCharArray();
        char[] ch2 = sb2.reverse().toString().toCharArray();
        boolean exchange = false;

        if (args2.length() > args.length() || (args2.length() == args.length() && args.charAt(0) < args2.charAt(0))) {
            char[] temp = ch;
            ch = ch2;
            ch2 = temp;
            exchange = true;
        }
        //减法没有进位，所以原址排序即可
        int decimal = 0;// 进位
        int temp = 0;

        //运算
        for (int i = 0; i < ch.length; i++) {
            if (i < ch2.length) {
                temp = (ch[i] - '0') - (ch2[i] - '0') + decimal;
            } else if (decimal == 0) {//设计的比较好
                break;
            } else {
                temp = (ch[i] - '0') + decimal;
            }
            if (temp < 0) {
                temp += 10;
                decimal = -1;
            } else {
                decimal = 0;
            }
            ch[i] = (char) (temp + '0');
        }
        StringBuilder sb3 = new StringBuilder(String.valueOf(ch));
        return exchange ? '-' + sb3.reverse().toString() : sb3.reverse().toString();
    }

    //大整数乘法【无符号】
    public String multiWithoutSymbol(String args, String args2) {
        StringBuilder sb = new StringBuilder(args);
        StringBuilder sb2 = new StringBuilder(args2);

        //字符串逆置，方便计算和存储
        char[] ch = sb.reverse().toString().toCharArray();
        char[] ch2 = sb2.reverse().toString().toCharArray();
        //两个数相乘的积位数不会大于两数位数之和
        int length = ch.length + ch2.length;
        int[] multiSum = new int[length];
        //运算
        for (int i = 0; i < ch.length; i++) {
            for (int j = 0; j < ch2.length; j++) {
                multiSum[i + j] += (ch[i] - '0') * (ch2[j] - '0');
                //数值整理和进位
                multiSum[i + j + 1] += multiSum[i + j] / 10;
                multiSum[i + j] %= 10;
            }
        }
        StringBuilder sb3 = new StringBuilder();
        //消除0
        boolean zeroFlag = false;
        for (int i = length - 1; i >= 0; i--) {
            if (multiSum[i] != 0) {
                zeroFlag = true;
            }
            if (zeroFlag) {
                sb3.append(multiSum[i]);
            }
        }
        return sb3.toString();
    }

    public String divWithoutSymbol(String args, String args2) {


        return "";
    }

    //大整数合并【无符号】
    public String mergeWithoutSymbol(String args, String args2) {
        StringBuilder sb = new StringBuilder();
        if (args.length() < args2.length()) {
            String temp = args;
            args = args2;
            args2 = temp;
        }
        int decimal = 0;// 进位
        for (int i = args.length() - 1; i >= 0; i--) {
            if (i < args2.length()) {
                int tempSum = (args.charAt(i) - '0' + args2.charAt(i)) - '0' + decimal;
                decimal = tempSum / 10;
                sb.append((char) (tempSum % 10 + '0'));
            } else {
                sb.append(args.charAt(i));
            }
        }
        // 最后一位表示进位
        sb.append(decimal);
        return sb.reverse().toString();
    }

    public boolean OperationCheck(String args, String args2) {
        // 异常和0值的判断
        if (args == null || args2 == null || args.length() == 0 || args2.length() == 0) {
            return false;
        }
        return true;
    }
}
